var SinaWeiBo = {
	id: null,
	app_key: null,
	app_secret: null,
	return_url: null,
	remove_url: null,
	area: null
};

$(function(){
	getWeiBoAppInfo();		//获取新浪微博应用信息
	getTokenList();			//获取授权集合
	eventListen();			//事件监听
});

/**
 * @note 获取新浪微博应用信息
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function getWeiBoAppInfo(){
	var ajaxPath = (window.location + "").split("/")[0] + "//" + (window.location + "").split("/")[2] + "/" + (window.location + "").split("/")[3];
	var data = {};
	data.area = "999999";
	$.ajax({
		url: ajaxPath + "/sina/getWeiBoAppInfo.do",
		async:false,
		type: "post",
		data: data,
		dataType: "json",
		//回调函数
		success:function(result){
			if(result.status >= 0){
				$("#edit_button").html("编&nbsp;&nbsp;辑");
				var value = result.data;
				SinaWeiBo.id = value[0].id;
				SinaWeiBo.app_key = value[0].app_key;
				SinaWeiBo.app_secret = value[0].app_secret;
				SinaWeiBo.return_url = value[0].return_url;
				SinaWeiBo.remove_url = value[0].remove_url;
				SinaWeiBo.area = value[0].area;
				$("#app_key_input").val(value[0].app_key);
				$("#app_secret_input").val(value[0].app_secret);
				$("#return_url_input").val(value[0].return_url);
				$("#remove_url_input").val(value[0].remove_url);
			}else{
				$("#edit_button").html("新&nbsp;&nbsp;增");
			}
		},
		error:function(){
			//异常处理
			window.console.log("获取新浪微博应用信息异常");
		}
	});
}

/**
 * @note 获取授权集合
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function getTokenList(){
	var ajaxPath = (window.location + "").split("/")[0] + "//" + (window.location + "").split("/")[2] + "/" + (window.location + "").split("/")[3];
	var data = {};
	data.appId = SinaWeiBo.id;
	$.ajax({
		url: ajaxPath + "/SinaService.do",
		type: "post",
		data:data,
		dataType: "json",
		//回调函数
		success:function(result){
			$("#add_token_div").empty();
			var html = "";
			html += "<div id=\"add_token\" class=\"authorize_add\" title=\"添加授权账号\">" +
            		"	<img class=\"center-block\" src=\"../resources/img/png/add.png\" alt=\"\">" +
       				"</div>";	
			if(result.length > 0){
				for(var i=0; i<result.length; i++){
					html += "<div class=\"weibotoken_div\">" +
							"	<img class=\"weibotoken_div_img\" src=\""+result[i].image+"\">" +
							"	<span class=\"weibotoken_div_name\">"+result[i].name+"</span>" +
							"	<span class=\"weibotoken_div_msg\" name=\"removeToken\" token=\""+result[i].token+"\" title=\"单击取消授权\">"+result[i].msg+"</span>" +
							"</div>";
				}
			}
			$("#add_token_div").append(html);
		},
		error:function(){
			//异常处理
			window.console.log("请求授权异常");
		}
	});
}

/**
 * @note 事件监听
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function eventListen(){
	$("#edit_button").on("click", editWeiBoAppInfo);
	$("#cancel_button").on("click", resetWeiBoAppInfo);
	$("#add_token_div").on("click", tokenDivClickFunc);
}

/**
 * @note 编辑新浪微博应用信息
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function editWeiBoAppInfo(){
	var ajaxPath = (window.location + "").split("/")[0] + "//" + (window.location + "").split("/")[2] + "/" + (window.location + "").split("/")[3];
	//编辑
	if(SinaWeiBo.id != null){
		if($("#app_key_input").val() == SinaWeiBo.app_key &&
		   $("#app_secret_input").val() == SinaWeiBo.app_secret &&
		   $("#return_url_input").val() == SinaWeiBo.return_url &&
		   $("#remove_url_input").val() == SinaWeiBo.remove_url){
			return;
		}else if($("#app_key_input").val() == ""){
			alert("APP KEY不能为空");
			return;
		}else if($("#app_secret_input").val() == ""){
			alert("APP SECRET不能为空");
			return;
		}else if($("#return_url_input").val() == ""){
			alert("授权回调页不能为空");
			return;
		}else if($("#remove_url_input").val() == ""){
			alert("取消授权回调页不能为空");
			return;
		}
		var data = {};
		data.id = SinaWeiBo.id;
		data.app_key = $("#app_key_input").val();
		data.app_secret = $("#app_secret_input").val();
		data.return_url = $("#return_url_input").val();
		data.remove_url = $("#remove_url_input").val();
		$.ajax({
			url: ajaxPath + "/sina/editWeiBoAppInfo.do",
			type: "post",
			data: data,
			dataType: "json",
			//回调函数
			success:function(result){
				alert(result.message);
				if(result.status >= 0){
					SinaWeiBo.app_key = $("#app_key_input").val();
					SinaWeiBo.app_secret = $("#app_secret_input").val();
					SinaWeiBo.return_url = $("#return_url_input").val();
					SinaWeiBo.remove_url = $("#remove_url_input").val();
				}
			},
			error:function(){
				//异常处理
				window.console.log("编辑新浪微博应用信息异常");
			}
		});
	}else{	//新增
		if($("#app_key_input").val() == ""){
			alert("APP KEY不能为空");
			return;
		}else if($("#app_secret_input").val() == ""){
			alert("APP SECRET不能为空");
			return;
		}else if($("#return_url_input").val() == ""){
			alert("授权回调页不能为空");
			return;
		}else if($("#remove_url_input").val() == ""){
			alert("取消授权回调页不能为空");
			return;
		}
		var data = {};
		data.app_key = $("#app_key_input").val();
		data.app_secret = $("#app_secret_input").val();
		data.return_url = $("#return_url_input").val();
		data.remove_url = $("#remove_url_input").val();
		$.ajax({
			url: ajaxPath + "/sina/addWeiBoAppInfo",
			type: "post",
			data: data,
			dataType: "json",
			//回调函数
			success:function(result){
				alert(result.message);
			},
			error:function(){
				//异常处理
				window.console.log("新增新浪微博应用信息异常");
			}
		});
	}
}

/**
 * @note 重置新浪微博应用信息
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function resetWeiBoAppInfo(){
	if(SinaWeiBo.id != null){
		$("#app_key_input").val(SinaWeiBo.app_key);
		$("#app_secret_input").val(SinaWeiBo.app_secret);
		$("#return_url_input").val(SinaWeiBo.return_url);
		$("#remove_url_input").val(SinaWeiBo.remove_url);
	}else{
		$("#app_key_input").val("");
		$("#app_secret_input").val("");
		$("#return_url_input").val("");
		$("#remove_url_input").val("");
	}
}

/**
 * @note 授权管理点击事件
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function tokenDivClickFunc(e){
	e = e || window.event;
	//增加授权
	if(e.target.id == "add_token" || e.target.parentNode.id == "add_token"){
		var tokenForm_param = "{id:'"+SinaWeiBo.id+"',app_key:'"+SinaWeiBo.app_key+"',app_secret:'"+SinaWeiBo.app_secret+"',return_url:'"+SinaWeiBo.return_url+"',remove_url:'"+SinaWeiBo.remove_url+"'}";
		$("#client_id").val(SinaWeiBo.app_key);
		$("#tokenForm_param").val(tokenForm_param);
		$("#redirect_uri").val(SinaWeiBo.return_url);
		document.tokenForm.submit();
	}
	//删除授权
	else if(e.target.attributes.name && e.target.attributes.name.nodeValue == "removeToken"){
		if(confirm("确认取消授权") == true){
			removeTokenFunc(e);
		}
	}
}

/**
 * @note 移除授权
 * @author 吴义迪
 * @create_time 2017-06-06
 */
function removeTokenFunc(e){
	var ajaxPath = (window.location + "").split("/")[0] + "//" + (window.location + "").split("/")[2] + "/" + (window.location + "").split("/")[3];
	var token = e.target.attributes.token.nodeValue
	$.ajax({
		url: ajaxPath + "/sina/removeToken.do",
		type: "post",
		data:{token:token},
		dataType: "json",
		//回调函数
		success:function(result){
			if(result.status >= 0){
				e.target.parentNode.parentNode.removeChild(e.target.parentNode);
			}else{
				alert(result.message);
			}
		},
		error:function(){
			//异常处理
			window.console.log("移除授权异常");
		}
	});
}